package com.nsn.auction.domina;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="OFFER")
public class Offer implements Serializable{
	
	private User buyer;
	private int offerValue;
	private Long offerId;
	
	public Offer() {}
	
	public Offer(User buyer, int offerValue) {
		this.buyer = buyer;
		this.offerValue = offerValue;
	}
	
	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public User getBuyer() {
		return buyer;
	}
	
	public void setBuyer(User buyer) {
		this.buyer = buyer;
	}
	
	@Column(name = "OFFER_VALUE", nullable = false, unique = false)
	public int getOfferValue() {
		return offerValue;
	}
	
	public void setOfferValue(int offerValue) {
		this.offerValue = offerValue;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="OFFER_ID", unique = true, nullable = false)
	public Long getOfferId() {
		return offerId;
	}
	
	public void setOfferId(Long offerId) {
		this.offerId = offerId;
	}
	
	@Override
	public boolean equals(Object object) {
		
		//check for selt-comparison
		if (this == object) return true;
		if (!( this.getClass().equals(object.getClass()) )) return false;
		
		Offer temp_offer = (Offer) object;
		 return (null == getBuyer() ? null == temp_offer.getBuyer() : getBuyer().equals(temp_offer.getBuyer()) && 
				0 == getOfferValue() ? 0 == temp_offer.getOfferValue() : getOfferValue() == temp_offer.getOfferValue());
	}
	

}
